<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp_lib/init.jsp"%>

<script type="text/javascript">
	function comp(){
		var pos_money = Ext.getCmp('pos_money').getValue();
		if( pos_money==null ){
			return;
		}
				
		var rate_money_comp = Ext.getCmp('rate_money_comp').getValue();
		var rate = Ext.getCmp('pos_rate').getValue();
		
		if( rate!=null ){
			rate_money_comp =  pos_money * rate / 100;
			Ext.getCmp('rate_money_comp').setValue(rate_money_comp);
			return;
		}
		
		/*
		if( rate_money_comp==null && rate!=null ){
			rate_money_comp =  pos_money * rate / 100;
			Ext.getCmp('rate_money_comp').setValue(rate_money_comp);
			return;
		}
		
		if( rate_money_comp!=null && rate==null ){
			rate =  rate_money_comp *100 / pos_money;
			Ext.getCmp('pos_rate').setValue(rate);
			return;
		}
		*/
	}//#comp

	var add_form_panel = Ext
			.create(
					"Ext.form.Panel",
					{
						url : "add.htm",
						buttonAlign : "center",
						bodyStyle : "padding: 5px;",
						defaultType : "textfield",
						layout : "form",
						items : [
								{
									xtype : "radiogroup",
									labelStyle : "text-align:right;",
									fieldLabel : "类型",
									items : [ {
										boxLabel : "还款",
										labelStyle : "text-align:right;",labelWidth : 60,
										name : "pos.type",
										inputValue : "还款"
									}, {
										boxLabel : "消费",
										labelStyle : "text-align:right;",labelWidth : 60,
										name : "pos.type",
										inputValue : "消费"
									} ],
									allowBlank : false,
									listeners : {
										'change' : function(radio, newV, oldV,
												e) {
											if (newV["pos.type"] == "还款") {
												Ext.getCmp('pos_rate_money')
														.show();
												Ext.getCmp('pos_rate_cont').hide();
											} else if (newV["pos.type"] == "消费") {
												Ext.getCmp('pos_rate_money')
														.hide();
												Ext.getCmp('pos_rate_cont').show();
											}
										}
									}
								},
								{
									fieldLabel : "金额",
									labelStyle : "text-align:right;",labelWidth : 60,
									id : "pos_money",
									name : "pos.money",
									xtype : "numberfield",
									allowBlank : false
								},
								{
									xtype : "fieldcontainer",
									layout : "column",
									id : 'pos_rate_cont',
									hidden : true,
									fieldLabel : "选择费率",
									items : [
											{
												labelStyle : "text-align:right;",labelWidth : 60,
												xtype : "combo",
												mode : 'local',
												id : "pos_rate",
												name : "pos.rate",
												value : '',
												regex : /^-?\d+(\.\d+)?$/,
												regexText : "只能输入数字",
												store : new Ext.data.SimpleStore(
														{
															fields : [ 'value', 'text' ],
															data : [
															    ['0','0'],
																[ '0.38', '0.38' ],
																[ '0.5', '0.5' ],
																[ '0.65', '0.65' ],
																[ '0.69', '0.69' ],
																[ '0.78', '0.78' ],
																[ '1', '1' ],
																[ '1.25', '1.25' ] ]
												}),
												listeners : {
													change:function(){
														comp();
													}
												}
											},{
												labelStyle : "text-align:right;",labelWidth : 65,
												id : "rate_money_comp",
												name : "rate_money_comp",
												xtype : "numberfield",
												fieldLabel : "<span class='red'>%</span> 费率成本",
												listeners : {
													change:function(){
														//comp();
													}
												}
											} ]
						}, {
							id : 'pos_rate_money',
							fieldLabel : "转账手续费",
							labelStyle : "text-align:right;",labelWidth : 60,
							name : "pos.rate_money",
							xtype : "numberfield",
							hidden : true
						}, {
							fieldLabel : "交易地点",
							labelStyle : "text-align:right;",labelWidth : 60,
							name : "pos.address",
							xtype : "textfield",
							allowBlank : false
						}, {
							fieldLabel : "备注",
							labelStyle : "text-align:right;",labelWidth : 60,
							name : "pos.note",
							xtype : "textareafield"
						}, {
							labelStyle : "text-align:right;",labelWidth : 60,
							name : "pos.customer_card_id",
							xtype : "textareafield",
							value : "${card.kid}",
							hidden : true
						}

				],
				buttons : [ {
					text : "保存",
					formBind : true, //only enabled once the form is valid
					disabled : true,
					handler : function() {
						var form = this.up("form").getForm();
						if (form.isValid()) {
							form.submit({
								waitMsg : "保存中...",
								success : function(form, action) {
									Ext.Msg.alert("提示", action.result.tip.msg);
									add_form_panel_win.close();
									dataStore.load();
									window.location.reload(true);

								},
								failure : function(form, action) {
									Ext.Msg.alert("提示", action.result.tip.msg);
								}
							});
						}
					}
				} ]
			});

	var add_form_panel_win = Ext.create("Ext.Window", {
		title : "流水添加",
		closeAction : "hide",
		items : add_form_panel
	});

	function myAdd() {
		add_form_panel.getForm().reset();
		add_form_panel_win.show();
	}
</script>
